package com.sugar.follow.up.module.business.controller.admin.patient.nfmzl;

import cn.hutool.core.util.StrUtil;
import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;
import com.sugar.follow.up.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.follow.up.module.business.controller.admin.patient.nfmzl.vo.PatientNfmzlPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.patient.nfmzl.vo.PatientNfmzlRespVO;
import com.sugar.follow.up.module.business.controller.admin.patient.nfmzl.vo.PatientNfmzlSaveReqVO;
import com.sugar.follow.up.module.business.dal.dataobject.patientnfmzl.PatientNfmzlDO;
import com.sugar.follow.up.module.business.dal.dataobject.patientnfmzl.dto.NfmzlZlxxDTO;
import com.sugar.follow.up.module.business.service.patientnfmzl.PatientNfmzlService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 内分泌治疗")
@RestController
@RequestMapping("/business/patient-nfmzl")
@Validated
public class PatientNfmzlController {

    @Resource
    private PatientNfmzlService patientNfmzlService;

    @PostMapping("/create")
    @Operation(summary = "创建内分泌治疗")
    //@PreAuthorize("@ss.hasPermission('business:patient-nfmzl:create')")
    public CommonResult<Long> createPatientNfmzl(@Valid @RequestBody PatientNfmzlSaveReqVO createReqVO) {
        return success(patientNfmzlService.createPatientNfmzl(createReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新内分泌治疗")
    //@PreAuthorize("@ss.hasPermission('business:patient-nfmzl:update')")
    public CommonResult<Boolean> updatePatientNfmzl(@Valid @RequestBody PatientNfmzlSaveReqVO updateReqVO) {
        patientNfmzlService.updatePatientNfmzl(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除内分泌治疗")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:patient-nfmzl:delete')")
    public CommonResult<Boolean> deletePatientNfmzl(@RequestParam("id") Long id) {
        patientNfmzlService.deletePatientNfmzl(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除内分泌治疗")
    //@PreAuthorize("@ss.hasPermission('business:patient-nfmzl:delete')")
    public CommonResult<Boolean> deletePatientNfmzlList(@RequestParam("ids") List<Long> ids) {
        patientNfmzlService.deletePatientNfmzlListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得内分泌治疗")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:patient-nfmzl:query')")
    public CommonResult<PatientNfmzlRespVO> getPatientNfmzl(@RequestParam("id") Long id) {
        PatientNfmzlDO patientNfmzl = patientNfmzlService.getPatientNfmzl(id);
        return success(BeanUtils.toBean(patientNfmzl, PatientNfmzlRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得内分泌治疗分页")
    //@PreAuthorize("@ss.hasPermission('business:patient-nfmzl:query')")
    public CommonResult<PageResult<PatientNfmzlRespVO>> getPatientNfmzlPage(@Valid PatientNfmzlPageReqVO pageReqVO) {
        PageResult<PatientNfmzlDO> pageResult = patientNfmzlService.getPatientNfmzlPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PatientNfmzlRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出内分泌治疗 Excel")
    //@PreAuthorize("@ss.hasPermission('business:patient-nfmzl:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPatientNfmzlExcel(@Valid PatientNfmzlPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PatientNfmzlDO> list = patientNfmzlService.getPatientNfmzlPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "内分泌治疗.xls", "数据", PatientNfmzlRespVO.class,
                BeanUtils.toBean(list, PatientNfmzlRespVO.class, o -> {
                    List<NfmzlZlxxDTO> zlxx = o.getZlxx();
                    StringBuilder builder = new StringBuilder();
                    for (NfmzlZlxxDTO nfmzlZlxxDTO : zlxx) {
                        builder.append(";")
                                .append(StrUtil.format("{}，第 {} 次治疗，去势：{}，抗雄：{}",
                                        nfmzlZlxxDTO.getDate(), nfmzlZlxxDTO.getCount(), nfmzlZlxxDTO.getQsfa(), nfmzlZlxxDTO.getKxfa()));
                    }
                    if (builder.length() > 0)
                        o.setZlxxStr(builder.substring(1));
                }));
    }

}